#include "tju_tcp.h"
#include <string.h>


int main(int argc, char **argv) {
    // 开启仿真环境 
    startSimulation();

    tju_tcp_t* my_socket = tju_socket();
    //调试用 11 行
    // printf("my_tcp state %d\n", my_socket->state); 
    
    tju_sock_addr target_addr;
    target_addr.ip = inet_network("172.17.0.3");
    target_addr.port = 1234;

    tju_connect(my_socket, target_addr);

    //调试用 20 ~ 31 行
    // printf("my_socket state %d\n", my_socket->state);      

    // uint32_t conn_ip;
    // uint16_t conn_port;

    // conn_ip = my_socket->established_local_addr.ip;
    // conn_port = my_socket->established_local_addr.port;
    // printf("my_socket established_local_addr ip %d port %d\n", conn_ip, conn_port);

    // conn_ip = my_socket->established_remote_addr.ip;
    // conn_port = my_socket->established_remote_addr.port;
    // printf("my_socket established_remote_addr ip %d port %d\n", conn_ip, conn_port);

    sleep(3);

    //tju_send 发送buffer的前20（header）+ len个字节到sock地址
    tju_send(my_socket, "i am client", 12);
    tju_send(my_socket, "hello server", 13);

    char buf[2021];
    //tju_recv 从sock地址接收len个字节到buffer
    tju_recv(my_socket, (void*)buf, 12);
    printf("client recv %s\n", buf);

    tju_recv(my_socket, (void*)buf, 10);
    printf("client recv %s\n", buf);

    // tju_close(my_socket);

    while(my_socket->state != CLOSED){
        sleep(1);
    }
    return EXIT_SUCCESS;
}
